﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.Common;
using HRProject.Code.DTO;
using System.Collections.Generic;

namespace HRProject.Role
{
    public partial class EditRole : System.Web.UI.Page
    {
        private Guid roleId = Guid.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            string roleIdStr = Request.Params["id"];
            try
            {
                roleId = new Guid(roleIdStr);
            }
            catch (Exception exp)
            {
                Console.Out.Write(exp.StackTrace);
                roleId = Guid.Empty;
            }
            if (!IsPostBack)
            {
                RoleDAO dao = new RoleDAO();
                BLRole filter = new BLRole();
                filter.Id = roleId;
                filter.Name=string.Empty;
                filter.Department=new BLDepartment();
                filter.Department.Id=Guid.Empty;
                List<BLRole> roles = dao.searchRolesInDepartment(filter);
                if (roles.Count > 0)
                {
                    tbxName.Text = roles[0].Name;
                    tbxDescription.Text = roles[0].Description;
                    imgIcon.ImageUrl = roles[0].IconUrl;
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!BLAuthentication.IsEditAllowed)
            {
                lblError.Text = ContentProvider.GetMessage("NotAuthorized");
                return;
            }

            string roleName = tbxName.Text.Trim();
            BLRole role = new BLRole();
            role.Id = roleId;
            role.Name=roleName;
            role.Description = tbxDescription.Text;
            role.DataSignature = 0;

            RoleDAO dao = new RoleDAO();
            BLRole changedRole = dao.getRole(roleName);
            bool allow = true;
            if (changedRole != null)
            {
                if (!changedRole.Id.Equals(roleId))
                {
                    allow = false;
                }
            }
            if (allow)
            {
                string iconUrl = string.Empty;
                if (cbxIcon.Checked && fulIcon.HasFile)
                {
                    try
                    {
                        string ext = fulIcon.PostedFile.FileName.Substring(fulIcon.PostedFile.FileName.LastIndexOf('.'));
                        iconUrl = "/image/role/" + role.Name + ext;
                        fulIcon.PostedFile.SaveAs(Server.MapPath(iconUrl));
                        role.IconUrl = iconUrl;
                    }
                    catch (Exception ex)
                    {
                        lblError.Text = "ERROR: " + ex.Message.ToString();
                    }
                }
                else
                {
                    lblError.Text = "You have not specified a file.";
                }

                dao.updateRole(role);
                Util.CloseFrame(Response);
            }
            else
            {
                lblError.Text = ContentProvider.GetMessage("AlreadyExistRoleError").Replace("{0}", roleName);
            }
        }
    }
}
